﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using JumbotCms.Common;
namespace JumbotCms.Modules.Article.User
{
    public partial class _edit : JumbotCms.API.UserCenter
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ChannelId = Str2Str(q("ccid"));
            id = Str2Str(q("id"));
            User_Load("", "html", true);
            checkEdit(id, "article");
            this.txtEditor.Text = UserName;
            this.txtUserId.Text = UserId;
            getEditDropDownList(ref ddlClassId, 0);
            //没有下面这段，浏览功能就失效
            Session["FCKeditor:UserUploadPath"] = site.Dir + ChannelDir + "/uploadfiles/";
            this.FCKeditor1.BasePath = site.Dir + "editors/fckeditor/";
            doh.Reset();
            Jumbot.DataOper.WebFormHandler wh = new Jumbot.DataOper.WebFormHandler(doh, "jcms_module_article", btnSave);
            wh.AddBind(txtTitle, "Title", true);
            wh.AddBind(ddlClassId, "ClassId", false);
            wh.AddBind(txtSourceFrom, "SourceFrom", true);
            wh.AddBind(txtAuthor, "Author", true);
            wh.AddBind(txtEditor, "Editor", true);
            wh.AddBind(txtUserId, "UserId", false);
            wh.AddBind(txtTags, "Tags", true);
            wh.AddBind(txtImg, "Img", true);
            wh.AddBind(rbtnContentTop, "SelectedValue", "IsTop", false);
            wh.AddBind(FCKeditor1, "Value", "Content", true);
            wh.AddBind(txtSummary, "Summary", true);
            wh.AddBind(ref ChannelId, "ChannelId", false);
            wh.AddBind(chkIsEdit, "1", "IsPass", false);
            wh.AddBind(txtAddDate, "AddDate", true);
            if (id == "0")
            {
                wh.Mode = Jumbot.DataOper.OperationType.Add;
                this.txtAddDate.Text = Jumbot.Common.staticFunction.GetNow("time");
            }
            else
            {
                wh.ConditionExpress = "UserId=" + UserId + " and [IsPass]=0 and id=" + id;
                wh.Mode = Jumbot.DataOper.OperationType.Modify;
            }
            wh.BindBeforeModifyOk += new EventHandler(bind_ok);
            wh.AddOk += new EventHandler(save_ok);
            wh.ModifyOk += new EventHandler(save_ok);
            wh.validator = chkForm;
        }
        /// <summary>
        /// 绑定数据后的处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bind_ok(object sender, EventArgs e)
        {
            this.txtSummary.Text = Jumbot.Common.Strings.HtmlDecode(this.txtSummary.Text);
        }
        protected bool chkForm()
        {
            if (!CheckFormUrl())
                return false;
            if (!Page.IsValid)
                return false;
            if (FCKeditor1.Value == "")
            {
                this.txtContentMsg.Text = "请填写内容!";
                return false;
            }
            return true;
        }
        protected void save_ok(object sender, EventArgs e)
        {
            if (id == "0")
            {
                Jumbot.DataOper.DbOperEventArgs de = (Jumbot.DataOper.DbOperEventArgs)e;
                id = de.id.ToString();
            }
            doh.Reset();
            doh.ConditionExpress = "id=@id";
            doh.AddConditionParameter("@id", id);
            if (txtImg.Text != "")
                doh.AddFieldItem("IsImg", "1");
            else
                doh.AddFieldItem("IsImg", "0");
            string[] setting = (string[])UserSetting.Split(',');
            bool _NeedCheck = (setting[10] == "1");
            if(_NeedCheck)
                doh.AddFieldItem("IsPass", 0);
            else
                doh.AddFieldItem("IsPass", 1);
            doh.Update("jcms_module_article");
            FinalMessage("成功保存", site.Dir + "user/otherfiles/close.htm", 0);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.txtTitle.Text = Jumbot.Common.Strings.SafetyStr(this.txtTitle.Text);
            if (this.txtSummary.Text.Length == 0)
                this.txtSummary.Text = GetCutString(Jumbot.Common.Strings.NoHTML(FCKeditor1.Value), 200);
            else
                this.txtSummary.Text = GetCutString(Jumbot.Common.Strings.HtmlEncode(this.txtSummary.Text), 200);
            //格式化标签
            this.txtTags.Text = Jumbot.Common.Strings.SafetyStr(this.txtTags.Text);
            //插入标签
            JumbotCms.DataObject.Tag tag = new JumbotCms.DataObject.Tag();
            tag.InsertTag(ChannelId, this.txtTags.Text, doh);
        }
    }
}
